package view;

import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import model.Contact;

/**
 *
 * @author Gautier
 */
public class ContactDisplay extends JPanel implements ActionListener, MouseListener {

    private JLabel img;
    private JLabel name;
    private JLabel fname;
    private JLabel address;
    private JLabel pPhone;
    private JLabel proPhone;
    private JLabel email;
    private JLabel home;

    private JTextField tName;
    private JTextField tFName;
    private JTextField tAddress;
    private JTextField tPPhone;
    private JTextField tProPhone;
    private JTextField tEmail;
    private JTextField tHome;

    private JButton bEmail;
    private JButton bHome;
    private JButton bEdit;
    private JButton bDelete;

    ArrayList<Contact> al;
    private Contact co;

    public ContactDisplay(ArrayList<Contact> a) {
        super();
        al = a;

        img = new JLabel();
        img.setIcon(new ImageIcon("img/default.jpg"));
        img.setPreferredSize(new Dimension(100, 100));

        name = new JLabel("Name :");
        tName = new JTextField(15);
        tName.setMinimumSize(new Dimension(80, 20));

        fname = new JLabel("First name :");
        tFName = new JTextField(15);
        tFName.setMinimumSize(new Dimension(80, 20));

        address = new JLabel("Address :");
        tAddress = new JTextField(30);
        tAddress.setMinimumSize(new Dimension(160, 20));

        pPhone = new JLabel("Phone :");
        tPPhone = new JTextField(30);
        tPPhone.setMinimumSize(new Dimension(160, 20));

        proPhone = new JLabel("Pro phone :");
        tProPhone = new JTextField(30);
        tProPhone.setMinimumSize(new Dimension(160, 20));

        email = new JLabel("Email :");
        tEmail = new JTextField(30);
        tEmail.setMinimumSize(new Dimension(160, 20));
        bEmail = new JButton("Message");
        bEmail.addActionListener(this);

        home = new JLabel("Home :");
        tHome = new JTextField(30);
        tHome.setMinimumSize(new Dimension(160, 20));
        bHome = new JButton("Visit");
        bHome.addActionListener(this);

        bEdit = new JButton("Edit");
        bEdit.addActionListener(this);
        bDelete = new JButton("Delete");
        bDelete.addActionListener(this);

        //Test with GridBagLayout
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();

        //Image
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 2;
        c.insets = new Insets(5, 5, 5, 5);
        c.fill = GridBagConstraints.NONE;
        this.add(img, c);
        img.addMouseListener(this);

        //Name
        c.gridx = 2;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(name, c);

        c.gridx = 3;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(tName, c);

        //FName
        c.gridx = 2;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(fname, c);

        c.gridx = 3;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(tFName, c);

        //Address
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(address, c);

        c.gridx = 1;
        c.gridy = 2;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(tAddress, c);

        //Phone perso
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(pPhone, c);

        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(tPPhone, c);

        //Pro phone
        c.gridx = 0;
        c.gridy = 4;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(proPhone, c);

        c.gridx = 1;
        c.gridy = 4;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(tProPhone, c);

        //Email
        c.gridx = 0;
        c.gridy = 5;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(email, c);

        c.gridx = 1;
        c.gridy = 5;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(tEmail, c);

        c.gridx = 3;
        c.gridy = 5;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(bEmail, c);

        //Home
        c.gridx = 0;
        c.gridy = 6;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(home, c);

        c.gridx = 1;
        c.gridy = 6;
        c.gridwidth = 2;
        c.gridheight = 1;
        this.add(tHome, c);

        c.gridx = 3;
        c.gridy = 6;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(bHome, c);

        c.gridx = 1;
        c.gridy = 7;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(bEdit, c);

        c.gridx = 2;
        c.gridy = 7;
        c.gridwidth = 1;
        c.gridheight = 1;
        this.add(bDelete, c);

        this.setVisible(true);
        this.repaint();
    }

    public void reset() {
        img.setIcon(new ImageIcon("img/default.jpg"));
        tName.setText("");
        tFName.setText("");
        tPPhone.setText("");
        tProPhone.setText("");
        tAddress.setText("");
        tHome.setText("");
        tEmail.setText("");
    }

    public void update(Contact c) {
        co = c;
        if (c.getImage().compareTo("") == 0) {
            img.setIcon(new ImageIcon("img/default.jpg"));
        } else {
            img.setIcon(new ImageIcon(c.getImage()));
        }
        tName.setText(c.getName());
        tFName.setText(c.getFirstName());
        tPPhone.setText(c.getPersonalPhone());
        tProPhone.setText(c.getProfessionalPhone());
        tAddress.setText(c.getHomeAddress());
        tHome.setText(c.getHomePage());
        tEmail.setText(c.getEmail());
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == bHome) {
            try {
                (Desktop.getDesktop()).browse(new URI(tHome.getText()));
            } catch (Exception ex) {
                Logger.getLogger(ContactDisplay.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (e.getSource() == bEmail) {
            try {
                (Desktop.getDesktop()).mail(new URI("mailto:" + tEmail.getText()));
            } catch (Exception ex) {
                Logger.getLogger(ContactDisplay.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (e.getSource() == bEdit) {
            co.editContact(tName.getText(), tFName.getText(), tAddress.getText(), tPPhone.getText(), tProPhone.getText(), tHome.getText(), tEmail.getText());
        }
        if (e.getSource() == bDelete) {
            al.remove(co);
            this.reset();
        }
        if (e.getSource() == img) {
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
            co.setImage(chooser.getSelectedFile().getAbsolutePath());
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == img) {
            JFileChooser chooser = new JFileChooser();
            chooser.showOpenDialog(null);
            co.setImage(chooser.getSelectedFile().getAbsolutePath());
            this.update(co);
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
